package com.jwd.avmanager.util;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

public class ByteUtil {
    //全部转int
    public static int wholeToInt(byte[] bytes) {
        byte[] bytes1 = new byte[4];
        if (bytes.length < 4) {
            System.arraycopy(bytes, 0, bytes1, 0, bytes.length);
        } else if (bytes.length == 4) {
            bytes1 = bytes;
        } else {
        }
        int i = bytesToInt(bytes1);
        return i;
    }


    //short转字节数组
    public static byte[] shortToBytes(short n) {
        byte[] b = new byte[2];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
//        b[2] = (byte) (n >> 16 & 0xff);
//        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }

    //字节数组转short
    public static short bytesToShort(byte[] bytes) {
        if (bytes.length != 2) return -1;
        short i = (short) 0;

        i |= (bytes[0] & 0xff);
        i |= ((bytes[1] & 0xff) << 8);
        return i;
    }


    //int 转byte数组
    public static byte[] intToBytes(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }

    //字节数组转int
    public static int bytesToInt(byte[] bytes) {
        int res = 0;
        for (int i = 0; i < bytes.length; i++) {
            res += (bytes[i] & 0xff) << i * 8;
        }
        return res;
    }


    //char 转 byte[] 数组

    public static byte[] charToByte(char c) {
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xFF00) >> 8);
        b[1] = (byte) (c & 0xFF);
        return b;
    }

    //byte[] 数组转 char

    public static char byteToChar(byte[] b) {
        char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
        return c;
    }

    //char[] 数组转 byte[]
    public static byte[] getBytes(char[] chars) {
        Charset cs = Charset.forName("gb2312");//设定字符集编码代号
        CharBuffer cb = CharBuffer.allocate(chars.length);//按照字符数组长度进行分配空间
        cb.put(chars); //装载数据
        cb.flip(); //指针复位
        //按照编码规则进行编码
        ByteBuffer bb = cs.encode(cb);
        return bb.array();
    }

    //byte[] 数组转 char[]
    public static char[] getChars(byte[] bytes) {
        Charset cs = Charset.forName("gb2312");//指定字符集编码
        ByteBuffer bb = ByteBuffer.allocate(bytes.length);
        bb.put(bytes);//装载数据
        bb.flip();//调整回指针为0
        CharBuffer cb = cs.decode(bb);//按照指定字符集进行解码
        return cb.array();//返回字符数组
    }

    /**
     * 数组转16进制
     * @param bytes
     * @return
     */
    public static String getHexStr(byte[] bytes) {
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if(hex.length() < 2){
                sb.append(0);
            }
            sb.append(hex);
        }
        return sb.toString();
    }
}
